package org.uma.kri.nyu.mor.almas;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.uma.kri.nyu.mor.almas.entity.Contacts;
import org.uma.kri.nyu.mor.almas.entity.Email;
import org.uma.kri.nyu.mor.almas.entity.Phone;
import org.uma.kri.nyu.mor.almas.util.ContactsUtil;
import org.uma.kri.nyu.mor.almas.util.EditBaseAdapter;
import org.uma.kri.nyu.mor.almas.util.EditGroupAdapter;

import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView.BufferType;

/**
 * 電話帳情報編集フラグメント
 * 
 * @author kri
 * 
 */
public class DetailEditFragment extends Fragment {

	public enum MultipleSetList {
		PHONE ("電話番号", 1),
		EMAIL ("メールアドレス", 2),
		EVENT ("記念日", 3);
		
		private final String _name;
		private final int _num;
		private MultipleSetList(String name, int num) {
			this._name = name;
			this._num = num;
		}
		public String Name() { return _name; }
		public int Num() { return _num; }
	};
	
	public DetailEditFragment() {
		// TODO 自動生成されたコンストラクター・スタブ
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO 自動生成されたメソッド・スタブ

		return inflater
				.inflate(R.layout.fragment_edit_detail, container, false);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO 自動生成されたメソッド・スタブ
		super.onActivityCreated(savedInstanceState);

		// 電話帳情報を取得
		List<Contacts> telephoneList = ContactsUtil
				.getContactsList("koremadetowatigai2syanokyoudou@docomo.ne.jp");
		Contacts sampleinfo = telephoneList.get(99);

		// 一覧画面から送られた情報が取得出来たかチェック
		if (telephoneList.size() == 0) {
			return;
		}

		// Fragmentから各Viewを取得
		EditText familyName = (EditText) getView().findViewById(
				R.id.infd_edit_familyName);
		EditText firstName = (EditText) getView().findViewById(
				R.id.infd_edit_firstName);
		EditText familyNameA = (EditText) getView().findViewById(
				R.id.infd_edit_familyNameAlt);
		EditText firstNameA = (EditText) getView().findViewById(
				R.id.infd_edit_firstNameAlt);
		Spinner ringTone = (Spinner) getView().findViewById(
				R.id.infd_spinner_ringtone);
		Spinner groupClass = (Spinner) getView().findViewById(
				R.id.infd_spinner_group);
		ExpandableListView exListView = (ExpandableListView) getView().findViewById(
				R.id.infd_exList_group);
		EditText nickName = (EditText) getView().findViewById(
				R.id.infd_edit_nickname);
		EditText memo = (EditText) getView().findViewById(R.id.infd_edit_memo);

		// 連絡先から情報を抽出してリストに登録
		// 名称情報を登録
		familyName.setText(sampleinfo.familyName, BufferType.NORMAL);
		firstName.setText(sampleinfo.firstName, BufferType.NORMAL);
		familyNameA.setText(sampleinfo.familyNameAlt, BufferType.NORMAL);
		firstNameA.setText(sampleinfo.firstNameAlt, BufferType.NORMAL);

		// 着信音を設定
		// コンテントプロバイダインスタンスを生成
		ContentResolver contentResolver = getActivity().getContentResolver();
		
		Cursor cursor = contentResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, 
				new String[]{ MediaStore.Audio.Media.ALBUM, MediaStore.Audio.Media.ARTIST
				, MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media._ID }, 
				null, null, null);

		if (cursor == null || (cursor != null && cursor.getCount() == 0)) {
			cursor = contentResolver.query(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, 
					new String[]{ MediaStore.Audio.Media.ALBUM, MediaStore.Audio.Media.ARTIST
					, MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media._ID }, 
					null, null, null);
		}
		// 端末に保存されている音楽ファイルを取得

		// スピナーにセットするアイテムリストを生成
		ArrayAdapter<String> ringToneAdapter = new ArrayAdapter<String>(
				getActivity().getApplication(), android.R.layout.simple_spinner_item);
		ringToneAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		
		// 取得した音楽ファイルの件数が１件以上存在する場合
		if (cursor.getCount() > 0) {
			// 曲名をスピナーに表示する
			while (cursor.moveToNext()) {
				// 対象データから曲のタイトルを取得
				ringToneAdapter.add(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE)));
			}
			ringTone.setAdapter(ringToneAdapter);
			ringTone.setPrompt(getActivity().getResources().getString(R.string.ringtone_sel));
		}
		
		// Groupの複数選択用スピナーを作成
		EditGroupAdapter groupAdapter = new EditGroupAdapter(
				getActivity().getApplication(), R.layout.editinfo_spinner_group, sampleinfo.groupList);
		groupAdapter
		.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		groupClass.setAdapter(groupAdapter);
//		groupClass.setPrompt(getResources().getString(R.string.groupClass_sel));

		// リストビューの親情報を設定
		List<String> parentList = new ArrayList<String>();
		for (MultipleSetList item : MultipleSetList.values()) {
			// 親リストに表示する内容を作成
			parentList.add(item.Name());
		}
		
		List<List<Map<String, String>>> childList = new ArrayList<List<Map<String,String>>>();
		// 子要素として表示する文字列を登録
		for (MultipleSetList item : MultipleSetList.values()) {
			List<Map<String, String>> childGroup = new ArrayList<Map<String, String>>();

			if (MultipleSetList.PHONE.Name().equals(item.Name())) {
				for(Phone phone : sampleinfo.phoneNoList) {
					Map<String, String> childData = new HashMap<String, String>();
					childData.put("TITLE", item.Name());
					childData.put("SUMMARY", phone.phoneNo);
					childGroup.add(childData);
				}
				childList.add(childGroup);
			} else if (MultipleSetList.EMAIL.Name().equals(item.Name())) {
				for(Email email : sampleinfo.mailAdressList) {
					Map<String, String> childData = new HashMap<String, String>();
					childData.put("TITLE", item.Name());
					childData.put("SUMMARY", email.mailAdress);
					childGroup.add(childData);
				}
				childList.add(childGroup);				
			} else {
				Map<String, String> childData = new HashMap<String, String>();
				childData.put("TITLE", "UNDEFIND");
				childData.put("SUMMARY", "NODATA");
				childGroup.add(childData);
				childList.add(childGroup);
			}
		}
		
		// グループ,電話番号,メールアドレス,記念日を表示
		EditBaseAdapter expandAdapter = new EditBaseAdapter(getActivity(), parentList, childList);
		exListView.setAdapter(expandAdapter);
		LinearLayout listLayout = (LinearLayout)getView().findViewById(R.id.infd_layout_list);
		listLayout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, 70 * 3));
		
		// 親要素のOpenイベント
		exListView.setOnGroupExpandListener(new OnGroupExpandListener() {
			ExpandableListView exList = (ExpandableListView) getView().findViewById(
					R.id.infd_exList_group);
			@Override
			public void onGroupExpand(int groupPosition) {
				LinearLayout listLayout = (LinearLayout)getView().findViewById(R.id.infd_layout_list);
				ExpandableListAdapter exAdapter = exList.getExpandableListAdapter();
				int nowHeight = listLayout.getHeight();
				listLayout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, 
						nowHeight + (80 * exAdapter.getChildrenCount(groupPosition))));
			}
		});
		
		// 親要素のCloseイベント
		exListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {
			ExpandableListView exList = (ExpandableListView) getView().findViewById(
					R.id.infd_exList_group);
			@Override
			public void onGroupCollapse(int groupPosition) {
				LinearLayout listLayout = (LinearLayout)getView().findViewById(R.id.infd_layout_list);
				ExpandableListAdapter exAdapter = exList.getExpandableListAdapter();
				int nowHeight = listLayout.getHeight();
				listLayout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, 
						nowHeight - (80 * exAdapter.getChildrenCount(groupPosition))));
			}
		});
		
		// ニックネーム
		
		// グループ設定イベント
	}
}